from django.shortcuts import render, redirect, reverse, HttpResponse
from web.forms.wiki import WikiForm,Wiki_editForm
from scripts.init_price_police import S28
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from scripts.cos_upload_demo import upload

def wiki(request, project_id):
    """wiki首页"""
    if request.method == 'GET':

        wiki_id = request.GET.get('wiki_id')  # 获取wiki的参数
        if not wiki_id or not wiki_id.isdecimal():  # 如果没有参数
            return render(request, 'wiki.html')
        if wiki_id.isdecimal(): # 如果他是正确的十进制数字
            if wiki_id: # 如果有值
                ret = S28().py_searche('select id,content from wiki where project_id="{0}" and id="{1}"'.format(request.obj[0][0], wiki_id))[0]
                return render(request, 'wiki.html', {'ret': ret})
        else:


            return render(request, 'wiki.html')  # 用户自己恶搞随便输入值


def wiki_add(request, project_id):
    """wiki添加"""
    if request.method == 'GET':
        form = WikiForm(project_id)
        return render(request, 'wiki_add.html', {'form': form})
    form = WikiForm(project_id, data=request.POST)
    if form.is_valid():
        title = form.cleaned_data.get('title')
        content = form.cleaned_data.get('content')
        father_id = form.cleaned_data.get('father_id')
        if father_id == 'null':
            S28().insert_into('wiki', {'title': title, 'content': content, 'project_id': project_id})
        else:
            S28().insert_into('wiki', {'title': title, 'content': content, 'project_id': project_id, 'father_id': father_id})
        url = reverse('wiki', kwargs={'project_id': project_id})
        return redirect(url)


def wiki_catalog(request, project_id):
    """wiki目录"""
    ret = S28().py_searche('select id,title,father_id from wiki where project_id="{0}"'.format(project_id))

    return JsonResponse({'status': True, 'ret': ret})


def wiki_delete(request, project_id, wiki_id):
    """删除文章"""
    S28().py_searche1('delete from wiki where project_id="{0}" and id="{1}"'.format(project_id, wiki_id))
    url = reverse('wiki', kwargs={'project_id': project_id})
    return redirect(url)


def wiki_edit(request, project_id, wiki_id):
    """编辑文章"""
    ret = S28().py_searche('select * from wiki where project_id="{0}" and id="{1}"'.format(project_id, wiki_id))
    if len(ret) == 0:  # 这个表示没有这个数据
        url = reverse('wiki', kwargs={'project_id': project_id})
        return redirect(url)
    if request.method == 'GET':
        form = Wiki_editForm(requested=project_id, wike_tuple=ret[0])
        return render(request, 'wiki_add.html', {'form': form, 'text': str(ret[0][2])})
    form = Wiki_editForm(data=request.POST, requested=project_id, wike_tuple=ret[0])
    if form.is_valid():
        title = form.cleaned_data.get('title')
        content = form.cleaned_data.get('content')
        father_id = form.cleaned_data.get('father_id')
        if father_id == 'null':
            S28().py_searche1('update wiki set title="{0}",content="{1}" where project_id="{2}" and id="{3}"'.format(title, content, project_id, wiki_id))
        else:
            S28().py_searche1('update wiki set title="{0}",content="{1}", father_id="{2}" where project_id="{3}" and id="{4}"'.format(title, content, father_id, project_id, wiki_id))
        url = reverse('wiki', kwargs={'project_id': project_id})
        return redirect(url)

@csrf_exempt
def wiki_upload(request, project_id):
    """上传图片"""
    print('收到消息了发来的图片了1！！')
    file = request.FILES.get('editormd-image-file')# 这个就是文件的对象
    key = file.name
    user_id = request.user_id
    # 文件对象上传到当前项目的桶中
    print(key)
    ret = upload(project_id, file, key)
    if ret:
        result = {
            'success': 1,
            'message': None,
            'url': ret
        }
        return JsonResponse(result)
